package com.android.fragmentdemo.definenotice;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;

//可以把Activity Fragment  Service等作为回调对象
public class NoticeManager {
    private static volatile NoticeManager mInstance;

    private static MainExecutor mExecutor;

    private static final Map<String, INoticeCallback> mNoticeCallbackMap = new HashMap<>();

    private static final List<String> mFilteredTagList = new ArrayList<>();

    private static final List<String> mFilteredKeyList = new ArrayList<>();

    private NoticeManager() {}

    public static NoticeManager getInstance() {
        if (mInstance == null) {
            synchronized (NoticeManager.class) {
                if (mInstance == null) {
                    mInstance = new NoticeManager();
                    mExecutor = new MainExecutor();
                }
            }
        }
        return mInstance;
    }

    public static class MainExecutor implements Executor {
        private static final Handler mHandler = new Handler(Looper.getMainLooper());
        @Override
        public void execute(Runnable command) {
            mHandler.post(command);
        }
    }

    public void addTag(String tag, INoticeCallback noticeCallback) {
        mNoticeCallbackMap.put(tag, noticeCallback);
    }

    public void removeTag(String tag) {
        mNoticeCallbackMap.remove(tag);
    }

    public void updateNotification(String key, Bundle bundle) {
        for (Map.Entry<String,INoticeCallback> entry : mNoticeCallbackMap.entrySet()) {
            String tag = entry.getKey();
            if (filteredTagNotAdded(tag)) {
                mExecutor.execute(() -> entry.getValue().notification(key, bundle));
            } else {
                if (filteredKeyNotAdded(key)) {
                    mExecutor.execute(() -> entry.getValue().notification(key, bundle));
                }
            }
        }
    }

    public void updateNotification(String key) {
        for (Map.Entry<String, INoticeCallback> entry : mNoticeCallbackMap.entrySet()) {
            String tag = entry.getKey();
            if (filteredTagNotAdded(tag)) {
                mExecutor.execute(() -> entry.getValue().notification(key, new Bundle()));
            } else {
                if (filteredKeyNotAdded(key)) {
                    mExecutor.execute(() -> entry.getValue().notification(key, new Bundle()));
                }
            }
        }
    }

    public void addFilteredTag(String tag) {
        boolean notAdded = filteredTagNotAdded(tag);
        if (notAdded) {
            mFilteredTagList.add(tag);
        }
    }

    public void removeFilteredTag(String tag) {
        boolean added = !filteredTagNotAdded(tag);
        if (added) {
            mFilteredTagList.removeIf(strTag -> strTag.equals(tag));
        }
    }

    public boolean filteredTagNotAdded(String tag) {
        boolean condition = true;
        for (String strTag : mFilteredTagList) {
            if (strTag.equals(tag)) {
                condition = false;
                break;
            }
        }
        return condition;
    }

    public void addFilteredKey(String key) {
        boolean notAdded = filteredKeyNotAdded(key);
        if (notAdded) {
            mFilteredTagList.add(key);
        }
    }

    public void removeFilteredKey(String key) {
        boolean added = !filteredKeyNotAdded(key);
        if (added) {
            mFilteredKeyList.removeIf(strKey -> strKey.equals(key));
        }
    }

    public boolean filteredKeyNotAdded(String key) {
        boolean condition = true;
        for (String strTag : mFilteredKeyList) {
            if (strTag.equals(key)) {
                condition = false;
                break;
            }
        }
        return condition;
    }
}
